/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app.view.gui;

import app.entities.model.DTO.NotasAlunoDTO;
import app.entities.model.DTO.TurmaDTO;
import app.view.classes.ViewAluno;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Murilo
 */
public class VerNotasAluno extends javax.swing.JFrame {

    private ViewAluno viewAluno;
    private String matriiculaAluno;
    private ArrayList<NotasAlunoDTO> listaNotas;

    public VerNotasAluno(String codigoAluno) {
        initComponents();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        pack();
        setLocation((screenSize.width/2) - (getWidth()/2), (screenSize.height/2) - (getHeight()/2));
        this.viewAluno = new ViewAluno();
        this.matriiculaAluno = codigoAluno;
        this.listaNotas = new ArrayList<>();
        carregarMateriasAluno();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnVerNotas = new javax.swing.JButton();
        cbTurmasMatriculadas = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        btnCancelar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblNotas = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Ver Notas - Aluno");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Matrícula"));

        btnVerNotas.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/view/gui/imgs/download.jpg"))); // NOI18N
        btnVerNotas.setText("Ver Notas");
        btnVerNotas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVerNotasActionPerformed(evt);
            }
        });

        jLabel2.setText("Matéria");

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/view/gui/imgs/images (2).jpg"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addGap(46, 46, 46)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnVerNotas)
                        .addGap(30, 30, 30)
                        .addComponent(btnCancelar))
                    .addComponent(cbTurmasMatriculadas, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(45, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(21, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbTurmasMatriculadas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnVerNotas))
                .addContainerGap())
        );

        tblNotas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Bimestre 1", "Bimestre 2", "Nota Final"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblNotas);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 12, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnVerNotasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVerNotasActionPerformed
        DefaultTableModel model = (DefaultTableModel) this.tblNotas.getModel();
        if(cbTurmasMatriculadas.getItemCount() == 0){
            JOptionPane.showMessageDialog(this, "Atenção! Você não tem notas lançadas nessa matéria.",
                        "Ver Notas", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        this.listaNotas = this.viewAluno.pesquisarNotasAluno(matriiculaAluno, 
                cbTurmasMatriculadas.getSelectedItem().toString());
        if(this.listaNotas.size() == 2){
            model.addRow(new Object[]{this.listaNotas.get(0).getValorNota(), this.listaNotas.get(1).getValorNota(),
                this.viewAluno.calcularNotaAluno(this.listaNotas.get(0).getValorNota(), this.listaNotas.get(1).getValorNota())});
        }
        else if(this.listaNotas.size() == 1){
            model.addRow(new Object[]{this.listaNotas.get(0).getValorNota(), 0, 
                this.viewAluno.calcularNotaAluno(this.listaNotas.get(0).getValorNota(), 0)});
        }
        else{
            JOptionPane.showMessageDialog(this, "Atenção! Você não tem notas lançadas nessa matéria.",
                        "Ver Notas", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_btnVerNotasActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnVerNotas;
    private javax.swing.JComboBox cbTurmasMatriculadas;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblNotas;
    // End of variables declaration//GEN-END:variables

    public void carregarMateriasAluno(){
        DefaultComboBoxModel cbModel = (DefaultComboBoxModel) this.cbTurmasMatriculadas.getModel();
        for (TurmaDTO turma : this.viewAluno.retornarListaTurmasAluno(this.matriiculaAluno)) {
            cbModel.addElement(turma.getNomeMateria());
        }
    }
}
